home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Communications / pcomm / Source / m_lib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-12  |  9.7 KB  |  404 lines

  1. /*
  2.  * Routines to manipulate the pcomm.modem file
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include "modem.h"
  7.  
  8. /*
  9.  * Read the modem/TTY database file.  Returns a pointer to a static area
  10.  * containing the MODEM structure.  All modem entries and all TTY entries
  11.  * are created regardless of the number of physical entries in the file.
  12.  */
  13.  
  14. struct MODEM *
  15. read_modem(extra)
  16. char *extra;
  17. {
  18.     extern char *null_ptr;
  19.     FILE *fp, *my_fopen();
  20.     int i, tty, mod, line, oops, m_line, start, stop;
  21.     char *str_dup(), buf[200], message[80], token[40], *str_tok(), *str;
  22.     char *temp_token, *t_sep, *m_sep, *m_letter, *findfile();
  23.     static struct MODEM m;
  24.     void error_win();
  25.  
  26.     if ((m.m_path = findfile(extra, "pcomm.modem")) == NULL)
  27.         error_win(1, "Support file \"pcomm.modem\" is missing", "or no read permission");
  28.  
  29.     if (!(fp = my_fopen(m.m_path, "r"))) {
  30.         sprintf(buf, "\"%s\" for read", m.m_path);
  31.         error_win(1, "Can't open modem/TTY file", buf);
  32.     }
  33.  
  34.     t_sep = ";;\n";
  35.     m_sep = ";;;;\n;;;;;;\n;;;\n";
  36.     m_letter = "abc";
  37.     oops = 0;
  38.     tty = 0;
  39.     mod = 0;
  40.     line = 0;
  41.     m_line = 0;
  42.     while (fgets(buf, 200, fp) != NULL) {
  43.         line++;
  44.         if (tty >= NUM_TTY || mod >= NUM_MODEM)
  45.             break;
  46.                     /* get the token */
  47.         if (!(temp_token = str_tok(buf, '='))) {
  48.             sprintf(message, "is missing a token at line %d", line);
  49.             oops++;
  50.             break;
  51.         }
  52.         if (*temp_token != 'T' && *temp_token != 'M') {
  53.             sprintf(message, "is corrupted at line %d", line);
  54.             oops++;
  55.             break;
  56.         }
  57.                     /* the TTY database */
  58.         if (*temp_token == 'T') {
  59.             /*
  60.              * This is similar to the "real" strtok() command
  61.              * but this one returns a pointer to NULL on a missing
  62.              * token.  Note the use of the field separator
  63.              * array.
  64.              */
  65.             for (i=0; i<3; i++) {
  66.                 if (!(str = str_tok((char *) NULL, t_sep[i]))) {
  67.                     sprintf(message, "is missing a parameter at line %d", line);
  68.                     oops++;
  69.                     break;
  70.                 }
  71.                 switch (i) {
  72.                     case 0:
  73.                         m.tty[tty] = str_dup(str);
  74.                         break;
  75.                     case 1:
  76.                         m.tname[tty] = str_dup(str);
  77.                         break;
  78.                     case 2:
  79.                         m.init_sp[tty] = atoi(str);
  80.                         break;
  81.                 }
  82.             }
  83.             if (oops)
  84.                 break;
  85.                     /* sanity checking */
  86.             sprintf(token, "TTY_%d", tty+1);
  87.             if (strcmp(token, temp_token)) {
  88.                 sprintf(message, "is corrupted at line %d", line);
  89.                 oops++;
  90.                 break;
  91.             }
  92.             tty++;
  93.             continue;
  94.         }
  95.                     /* the modem database */
  96.         else {
  97.             sprintf(token, "MODEM_%d%c", mod+1, m_letter[m_line]);
  98.             if (strcmp(token, temp_token)) {
  99.                 sprintf(message, "is corrupted at line %d", line);
  100.                 oops++;
  101.                 break;
  102.             }
  103.             /*
  104.              * There are three lines to the modem database.  They
  105.              * are distinguished by the letters a, b, and, c
  106.              * appended to the entry number.
  107.              */
  108.             switch (m_line) {
  109.                 case 0:
  110.                     start = 0;
  111.                     stop = 5;
  112.                     break;
  113.                 case 1:
  114.                     start = 5;
  115.                     stop = 12;
  116.                     break;
  117.                 case 2:
  118.                     start = 12;
  119.                     stop = 16;
  120.                     break;
  121.             }
  122.             for (i=start; i<stop; i++) {
  123.                 if (!(str = str_tok((char *) NULL, m_sep[i]))) {
  124.                     sprintf(message, "is missing a parameter at line %d", line);
  125.                     oops++;
  126.                     break;
  127.                 }
  128.                 switch (i) {
  129.                     case 0:
  130.                         m.mname[mod] = str_dup(str);
  131.                         break;
  132.                     case 1:
  133.                         m.init[mod] = str_dup(str);
  134.                         break;
  135.                     case 2:
  136.                         m.dial[mod] = str_dup(str);
  137.                         break;
  138.                     case 3:
  139.                         m.suffix[mod] = str_dup(str);
  140.                         break;
  141.                     case 4:
  142.                         m.hang_up[mod] = str_dup(str);
  143.                         break;
  144.                     case 5:
  145.                         m.auto_baud[mod] = *str;
  146.                         break;
  147.                     case 6:
  148.                         m.con_3[mod] = str_dup(str);
  149.                         break;
  150.                     case 7:
  151.                         m.con_12[mod] = str_dup(str);
  152.                         break;
  153.                     case 8:
  154.                         m.con_24[mod] = str_dup(str);
  155.                         break;
  156.                     case 9:
  157.                         m.con_48[mod] = str_dup(str);
  158.                         break;
  159.                     case 10:
  160.                         m.con_96[mod] = str_dup(str);
  161.                         break;
  162.                     case 11:
  163.                         m.con_192[mod] = str_dup(str);
  164.                         break;
  165.                     case 12:
  166.                         m.no_con1[mod] = str_dup(str);
  167.                         break;
  168.                     case 13:
  169.                         m.no_con2[mod] = str_dup(str);
  170.                         break;
  171.                     case 14:
  172.                         m.no_con3[mod] = str_dup(str);
  173.                         break;
  174.                     case 15:
  175.                         m.no_con4[mod] = str_dup(str);
  176.                         break;
  177.                 }
  178.             }
  179.             if (oops)
  180.                 break;
  181.             m_line++;
  182.             if (m_line >= 3) {
  183.                 m_line = 0;
  184.                 mod++;
  185.             }
  186.         }
  187.     }
  188.     fclose(fp);
  189.  
  190.     if (oops) {
  191.         sprintf(buf, "Modem/TTY database file \"%s\"", m.m_path);
  192.         error_win(1, buf, message);
  193.     }
  194.     m.t_entries = tty;
  195.     m.m_entries = mod;
  196.     m.t_cur = -1;
  197.     m.m_cur = -1;
  198.                     /* if empty database */
  199.     if (!tty) {
  200.         sprintf(buf, "Modem/TTY database file \"%s\"", m.m_path);
  201.         error_win(0, buf, "has no TTY data");
  202.     }
  203.     if (!mod) {
  204.         sprintf(buf, "Modem/TTY database file \"%s\"", m.m_path);
  205.         error_win(0, buf, "has no modem data");
  206.     }
  207.                     /* fill in the rest */
  208.     for (; tty<NUM_TTY; tty++) {
  209.         m.tty[tty] = null_ptr;
  210.         m.tname[tty] = null_ptr;
  211.         m.init_sp[tty] = 0;
  212.     }
  213.     for (; mod<NUM_MODEM; mod++) {
  214.         m.mname[mod] = null_ptr;
  215.         m.init[mod] = null_ptr;
  216.         m.dial[mod] = null_ptr;
  217.         m.suffix[mod] = null_ptr;
  218.         m.hang_up[mod] = null_ptr;
  219.  
  220.         m.auto_baud[mod] = 'Y';
  221.         m.con_3[mod] = null_ptr;
  222.         m.con_12[mod] = null_ptr;
  223.         m.con_24[mod] = null_ptr;
  224.         m.con_48[mod] = null_ptr;
  225.         m.con_96[mod] = null_ptr;
  226.         m.con_192[mod] = null_ptr;
  227.  
  228.         m.no_con1[mod] = null_ptr;
  229.         m.no_con2[mod] = null_ptr;
  230.         m.no_con3[mod] = null_ptr;
  231.         m.no_con4[mod] = null_ptr;
  232.     }
  233.     return(&m);
  234. }
  235.  
  236. /*
  237.  * Update the modem database.  Other routines actually do the changes
  238.  * or deletions in memory.  A non-zero return code means non-fatal error.
  239.  */
  240.  
  241. int
  242. up_modem()
  243. {
  244.     FILE *fp, *my_fopen();
  245.     char buf[80];
  246.     int i;
  247.     void error_win();
  248.  
  249.                     /* open for write */
  250.     if (!(fp = my_fopen(modem->m_path, "w"))) {
  251.         sprintf(buf, "\"%s\"", modem->m_path);
  252.         error_win(0, "No write permission on modem/TTY database file", buf);
  253.         return(1);
  254.     }
  255.                     /* put back the TTY entries */
  256.     for (i=0; i<modem->t_entries; i++)
  257.         fprintf(fp, "TTY_%d=%s;%s;%d\n", i+1, modem->tty[i],
  258.          modem->tname[i], modem->init_sp[i]);
  259.  
  260.                     /* put back the modem entries */
  261.     for (i=0; i<modem->m_entries; i++) {
  262.         fprintf(fp, "MODEM_%da=%s;%s;%s;%s;%s\n", i+1, modem->mname[i],
  263.          modem->init[i], modem->dial[i], modem->suffix[i],
  264.          modem->hang_up[i]);
  265.  
  266.         fprintf(fp, "MODEM_%db=%c;%s;%s;%s;%s;%s;%s\n", i+1,
  267.          modem->auto_baud[i], modem->con_3[i], modem->con_12[i],
  268.          modem->con_24[i], modem->con_48[i], modem->con_96[i],
  269.          modem->con_192[i]);
  270.  
  271.         fprintf(fp, "MODEM_%dc=%s;%s;%s;%s\n", i+1, modem->no_con1[i],
  272.          modem->no_con2[i], modem->no_con3[i], modem->no_con4[i]);
  273.     }
  274.  
  275.     fclose(fp);
  276.     return(0);
  277. }
  278.  
  279. /*
  280.  * See if the new modem is already in the database.  If it's not, create
  281.  * a slot for it and update the modem->m_cur variable.
  282.  */
  283.  
  284. void
  285. create_modem(str)
  286. char *str;
  287. {
  288.     int i;
  289.     char *str_rep(), buf[80];
  290.     void error_win();
  291.                     /* modem entry already exists? */
  292.     for (i=0; i<modem->m_entries; i++) {
  293.         if (!strcmp(str, modem->mname[i]))
  294.             return;
  295.     }
  296.                     /* empty slot available? */
  297.     if (modem->m_entries == NUM_MODEM) {
  298.         sprintf(buf, "\"%s\"", modem->m_path);
  299.         error_win(0, "No empty modem slots in", buf);
  300.         return;
  301.     }
  302.                     /* create a new entry */
  303.     i = modem->m_entries;
  304.     modem->mname[i] = str_rep(modem->mname[i], str);
  305.  
  306.                     /* update number of entries */
  307.     modem->m_entries++;
  308.     return;
  309. }
  310.  
  311. /*
  312.  * See if the modem names in the list still need to be in the database.
  313.  * If you find a "lost" entry, delete it and collapse the list.
  314.  */
  315.  
  316. void
  317. del_modem()
  318. {
  319.     extern char *null_ptr;
  320.     int i, j, match;
  321.     char *str_rep();
  322.     void free_ptr();
  323.  
  324.     for (i=0; i<modem->m_entries; i++) {
  325.         match = 0;
  326.         for (j=0; j<modem->t_entries; j++) {
  327.             if (!strcmp(modem->mname[i], modem->tname[j])) {
  328.                 match++;
  329.                 break;
  330.             }
  331.         }
  332.                     /* found a "lost" modem name */
  333.         if (!match) {
  334.             for (j=i; j<modem->m_entries-1; j++) {
  335.                     /* copy the info */
  336.                 modem->mname[j] = str_rep(modem->mname[j], modem->mname[j+1]);
  337.                 modem->init[j] = str_rep(modem->init[j], modem->init[j+1]);
  338.                 modem->dial[j] = str_rep(modem->dial[j], modem->dial[j+1]);
  339.                 modem->suffix[j] = str_rep(modem->suffix[j], modem->suffix[j+1]);
  340.                 modem->hang_up[j] = str_rep(modem->hang_up[j], modem->hang_up[j+1]);
  341.  
  342.                 modem->auto_baud[j] = modem->auto_baud[j+1];
  343.                 modem->con_3[j] = str_rep(modem->con_3[j], modem->con_3[j+1]);
  344.                 modem->con_12[j] = str_rep(modem->con_12[j], modem->con_12[j+1]);
  345.                 modem->con_24[j] = str_rep(modem->con_24[j], modem->con_24[j+1]);
  346.                 modem->con_48[j] = str_rep(modem->con_48[j], modem->con_48[j+1]);
  347.                 modem->con_96[j] = str_rep(modem->con_96[j], modem->con_96[j+1]);
  348.                 modem->con_192[j] = str_rep(modem->con_192[j], modem->con_192[j+1]);
  349.  
  350.                 modem->no_con1[j] = str_rep(modem->no_con1[j], modem->no_con1[j+1]);
  351.                 modem->no_con2[j] = str_rep(modem->no_con2[j], modem->no_con2[j+1]);
  352.                 modem->no_con3[j] = str_rep(modem->no_con3[j], modem->no_con3[j+1]);
  353.                 modem->no_con4[j] = str_rep(modem->no_con4[j], modem->no_con4[j+1]);
  354.             }
  355.             j = modem->m_entries -1;
  356.  
  357.             free_ptr(modem->mname[j]);
  358.             free_ptr(modem->init[j]);
  359.             free_ptr(modem->dial[j]);
  360.             free_ptr(modem->suffix[j]);
  361.             free_ptr(modem->hang_up[j]);
  362.  
  363.             free_ptr(modem->con_3[j]);
  364.             free_ptr(modem->con_12[j]);
  365.             free_ptr(modem->con_24[j]);
  366.             free_ptr(modem->con_48[j]);
  367.             free_ptr(modem->con_96[j]);
  368.             free_ptr(modem->con_192[j]);
  369.  
  370.             free_ptr(modem->no_con1[j]);
  371.             free_ptr(modem->no_con2[j]);
  372.             free_ptr(modem->no_con3[j]);
  373.             free_ptr(modem->no_con4[j]);
  374.  
  375.                     /* create an empty entry */
  376.             modem->mname[j] = null_ptr;
  377.             modem->init[j] = null_ptr;
  378.             modem->dial[j] = null_ptr;
  379.             modem->suffix[j] = null_ptr;
  380.             modem->hang_up[j] = null_ptr;
  381.  
  382.             modem->auto_baud[j] = 'Y';
  383.             modem->con_3[j] = null_ptr;
  384.             modem->con_12[j] = null_ptr;
  385.             modem->con_24[j] = null_ptr;
  386.             modem->con_48[j] = null_ptr;
  387.             modem->con_96[j] = null_ptr;
  388.             modem->con_192[j] = null_ptr;
  389.  
  390.             modem->no_con1[j] = null_ptr;
  391.             modem->no_con2[j] = null_ptr;
  392.             modem->no_con3[j] = null_ptr;
  393.             modem->no_con4[j] = null_ptr;
  394.  
  395.                     /* update the counts */
  396.             modem->m_entries--;
  397.             if (modem->m_cur >= modem->m_entries)
  398.                 modem->m_cur = -1;
  399.             return;
  400.         }
  401.     }
  402.     return;
  403. }
  404.